<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:mallNew:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:mallNew:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:mallNew:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:mallNew:export']"
        >导出</el-button> -->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallNewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="商城活动资讯id" align="center" prop="id" /> -->
      <el-table-column label="分类" align="center" prop="classId" :show-overflow-tooltip="true"/>
      <el-table-column label="名字" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="摘要" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column label="作者" align="center" prop="auther" :show-overflow-tooltip="true"/>
      <el-table-column label="来源" align="center" prop="source" :show-overflow-tooltip="true"/>
      <el-table-column label="0默认1热门" align="center" prop="hot" :formatter="hotFormat" />
      <el-table-column label="创建时间" align="center" prop="caAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.caAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="upAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.upAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:mallNew:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallNew:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商城活动咨询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="分类id" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入分类id" />
        </el-form-item> -->
        <!-- <el-form-item label="分类" prop="classId">
          <treeselect v-model="form.classId" :options="wxGoodsClassOptions" :normalizer="normalizer" placeholder="请选择分类" />
        </el-form-item> -->
        <!-- :disabled="item.status == 1" -->
            <el-form-item label="分类">
              <el-select v-model="form.classId" placeholder="请选择">
                <el-option
                  v-for="item in classOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"

                ></el-option>
              </el-select>
            </el-form-item>

        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" maxlength="50"/>
        </el-form-item>
        <el-form-item label="摘要" prop="title">
          <el-input v-model="form.title" placeholder="请输入摘要" maxlength="250"/>
        </el-form-item>
        <el-form-item label="作者" prop="auther">
          <el-input v-model="form.auther" placeholder="请输入作者" maxlength="50"/>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" maxlength="50"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="默认/热门">
          <el-radio-group v-model="form.hot">
            <el-radio
              v-for="dict in hotOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="caAt">
          <el-date-picker clearable size="small"
            v-model="form.caAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="upAt">
          <el-date-picker clearable size="small"
            v-model="form.upAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="0默认1删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入0默认1删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMallNew, getMallNew, delMallNew, addMallNew, updateMallNew, exportMallNew } from "@/api/mall/mallNew";
import Editor from '@/components/Editor';


export default {
  name: "MallNew",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城活动咨询表格数据
      mallNewList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 0默认1热门字典
      hotOptions: [],
      // 分类选项
      classOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名字不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "摘要不能为空", trigger: "blur" }
        ],
        hot: [
          { required: true, message: "默认/热门不能为空", trigger: "blur" }
        ],
        // caAt: [
        //   { required: true, message: "创建时间不能为空", trigger: "blur" }
        // ],
        // delFlag: [
        //   { required: true, message: "0默认1删除不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("mall_new_hot").then(response => {
      this.hotOptions = response.data;
    });
  },
  methods: {
    /** 查询商城活动咨询列表 */
    getList() {
      this.loading = true;
      listMallNew(this.queryParams).then(response => {
        this.mallNewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 0默认1热门字典翻译
    hotFormat(row, column) {
      return this.selectDictLabel(this.hotOptions, row.hot);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classId: null,
        name: null,
        title: null,
        auther: null,
        source: null,
        content: null,
        hot: 0,
        caAt: null,
        upAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      getMallNew().then(response => {
        this.form = response.data||{};
        this.open = true;
         this.classOptions = response.classList;
        this.title = "添加商城活动咨询";
      });
      // this.open = true;
      //  this.classOptions = response.classList;
      //  console.log(response.classList);
      // this.title = "添加商城活动咨询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMallNew(id).then(response => {
        this.form = response.data;
        this.open = true;
         this.classOptions = response.classList;
        this.title = "修改商城活动咨询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMallNew(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallNew(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商城活动咨询选中数据', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallNew(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商城活动咨询数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMallNew(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
